package com.acknight.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.acknight.dto.Result;
import com.acknight.dto.UserDTO;
import com.acknight.entity.Blog;
import com.acknight.service.IBlogService;
import com.acknight.service.IUserService;
import com.acknight.constant.SystemConstants;
import com.acknight.utils.UserHolder;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/blog")
@CrossOrigin
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).eq("is_delete", 0).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }

    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id)
                .eq("is_delete", 0)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));

        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {
        blog.setImages(blog.getImages().replace("[", "").replace("]", "").replace("\"", ""));
        blogService.save(blog);
        return Result.ok();
    }

    @GetMapping("/shopblog/{id}")
    public Result getBlogByShopId(@PathVariable("id") Long id) {
        return blogService.getBlogByShopId(id);
    }

    @GetMapping("/user/{id}")
    public Result getBlogByUserId(@PathVariable("id") Long id) {
        return blogService.getBlogByUserId(id);
    }

    @DeleteMapping("/{id}")
    public Result deleteBlogById(@PathVariable("id") Long id) {
        return blogService.deleteBlogById(id);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Blog blog) {
        blog.setImages(blog.getImages().replace("[", "").replace("]", "").replace("\"", ""));
        Blog result = blogService.getById(blog.getId());
        if (result == null) {
            return Result.fail("改博客不存在!");
        }
        if (!blog.getTitle().equals("")) {
            result.setTitle(blog.getTitle());
        }
        if (blog.getShopId() != null) {
            result.setShopId(blog.getShopId());
        }
        if (!blog.getContent().equals("")) {
            result.setContent(blog.getContent());
        }
        if (blog.getImages() != null && !blog.getImages().equals("")) {
            result.setImages(blog.getImages());
        }
        blogService.updateById(result);
        return Result.ok();
    }
}
